<template>
	<view class="content">
		<pai-pay-platform ref="alipayPlatform" btnBg="#3685FF" @payInfo="payInfo"></pai-pay-platform>
		<text class="alipay-hint">由于支付宝安全限制，支付成功后，需要手动关闭支付宝！</text>
		<!-- <text>数据：{{Textparams}}</text> -->
	</view>
</template>

<script>
	/**
	 * 支付宝支付页面
	 * */
	import {
		paiPayPlatform
	} from '../pai-pay-platform/pai-pay-platform.vue'
	// import encode from '../../tools/encode.js'

	export default {
		components: {
			paiPayPlatform
		},
		data() {
			return {
				query: {},
				info: {},
				Textparams: ""
			}
		},
		methods: {
			initData(option) {
				setTimeout(() => {
					// this.Textparams = 'option：' + JSON.stringify(option)
					let params = uni.getStorageSync("query")
					this.getUrlQuery(params);
					if (!this.info.key) {
						this.toast("订单异常，请联系管理员")
					}
					this.$refs.alipayPlatform.initData(this.info.key)
				}, 1000);
			},
			toast(text) {
				uni.showToast({
					title: text,
					duration: 1200
				})
			},
			getUrlQuery(params) {
				if (!params || !params.key || !params.tk) {
					return
				}
				uni.setStorageSync("query", {})
				// this.Textparams = 'params：' + JSON.stringify(params)
				// let token = params.st ? encode.decodeToken(params.st) : ""
				uni.setStorageSync('basePath', params.path)
				uni.setStorageSync('token', "Bearer" + " " + params.tk)
				this.info.key = params.key;
				this.query = JSON.stringify(params)
			},
			payInfo(payInfo) {
				// this.Textparams = "payInfo:" + JSON.stringify(payInfo)
				uni.requestPayment({
					provider: "alipay",
					orderInfo: payInfo.tradeNO,
					success: (res) => {
						// this.Textparams = "suc:" + JSON.stringify(res)
						if (res && res.resultCode === '9000') {
							// 支付成功 执行想要的操作
							this.$refs.alipayPlatform.paySuccessCallback(true)
							this.$refs.alipayPlatform.payResultCallback("支付成功")
						} else {
							this.$refs.alipayPlatform.paySuccessCallback(false)
							this.$refs.alipayPlatform.payResultCallback("失败")
						}
					},
					fail: (err) => {
						this.$refs.alipayPlatform.paySuccessCallback(false)
						// this.Textparams = "err:" + JSON.stringify(err)
						this.$refs.alipayPlatform.payResultCallback("失败")
					}
				})
			}
		}
	}
</script>

<style>
	page {
		height: 100%;
		width: 100%;
		background-color: #F7F7F7;
	}

	.alipay-hint {
		height: 60rpx;
		width: 100%;
		text-align: center;
		padding: 16rpx;
		color: rgba(0, 0, 0, 0.65);
		font-size: 24rpx;
	}

	.content {
		min-height: 100%;
		width: 100%;
		display: flex;
		flex-direction: column;
	}
</style>